import { ImageUrl } from '@/utils';
import { FormattedMessage, history, useIntl } from '@umijs/max';
import { Avatar, Button, Descriptions, DescriptionsProps, Rate } from 'antd';
import { useEffect, useState } from 'react';
type IInfo = {
    info: any;
    showInfoBtn?: boolean;
    release_id?: number;
    type?: string;
};
const UserInfo = (props: IInfo) => {
    const intl = useIntl();
    const [items, setItems] = useState<DescriptionsProps['items']>([]);
    function toDetail(){
        history.push('/detail',{
            id: props.release_id,
            type: 2,
            releaseType: props.type
        })
    }
    useEffect(() => {
        if (props.info) {
            const info = props.info;
            setItems([
                {
                    label: <FormattedMessage id="头像"></FormattedMessage>,
                    children: (
                        <div>
                            <Avatar
                                src={
                                    <img
                                        src={ImageUrl(info.avatar) || ''}
                                        alt="avatar"
                                    />
                                }
                            />
                        </div>
                    ),
                },
                {
                    label: <FormattedMessage id="名称"></FormattedMessage>,
                    children: (
                        <div>
                            {info[intl.formatMessage({ id: 'item.username' })]}
                        </div>
                    ),
                },
                {
                    label: <FormattedMessage id="标签"></FormattedMessage>,
                    children: (
                        <div>
                            <Avatar.Group>
                                {info.label.map((item) => (
                                    <Avatar
                                        key={item.url}
                                        shape="square"
                                        src={
                                            <img
                                                src={ImageUrl(item.url) || ''}
                                                alt="avatar"
                                            />
                                        }
                                    />
                                ))}
                            </Avatar.Group>
                        </div>
                    ),
                },
                {
                    label: <FormattedMessage id="综合评价"></FormattedMessage>,
                    children: (
                        <div>
                            <Rate
                                disabled
                                defaultValue={parseInt(info.rating / 20)}
                            />
                        </div>
                    ),
                },
            ]);
        }
    }, [props.info]);
    return (
        <>
            <Descriptions
                title={<FormattedMessage id="基本信息"></FormattedMessage>}
                extra={
                    props.showInfoBtn && (
                        <Button type="link" onClick={toDetail}>
                            <FormattedMessage id="查看详情"></FormattedMessage>
                        </Button>
                    )
                }
                items={items}
            />
        </>
    );
};
export default UserInfo;
